﻿using FPSO.Models.Constant;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FPSO.HMI.Controls
{
    /// <summary>
    /// UCSelectTank.xaml 的交互逻辑
    /// </summary>
    public partial class UCSelectTank : UserControl
    {
        private string TankIdStr = "";
        public UCSelectTank()
        {
            InitializeComponent();
            TankIdStr = "";
        }

        [Category("Action")]
        [Description("选择的舱室改变")]
        public event Action<string> OnTankChanged;

        private void btnTank_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen)
            {
                popChioce.IsOpen = false;
            }

            UCSelectTankView tankView = new UCSelectTankView();
            tankView.TankSelected += (tankId) =>
            {
                if (tankId != "")
                {
                    TankIdStr = tankId;
                    btnTank.Content = TankConstant.GetTankName(tankId);
                }
                else if (tankId == "")
                {
                    //清空
                    TankIdStr = "";
                    btnTank.Content = "";
                }
                //else 取消，保持之前的值

                popChioce.IsOpen = false;
                OnTankChanged?.Invoke(TankIdStr);
            };
            popChioce.Child = tankView;
            popChioce.Width = 550;
            popChioce.Height = 260;
            popChioce.IsOpen = true;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            if (TankIdStr == "")
            {
                btnTank.Content = "";
            }
            else
            {
                btnTank.Content = TankConstant.GetTankName(TankIdStr);
            }
        }

        public void UpdateText()
        {
            if (TankIdStr == "")
            {
                btnTank.Content = "";
            }
            else
            {
                btnTank.Content = TankConstant.GetTankName(TankIdStr);
            }
        }

        public string TankId { get { return TankIdStr; } set { TankIdStr = value; UpdateText(); } }
    }
}
